/*********************************************************************
Purpose: 	Run regressions with phone survey and baseline survey data.
*********************************************************************/

set more off
clear matrix
clear all
pause on
*ssc install estout
	
*------------------------------------------------------------------------

use "$PHONE_DATA_OUT/PhoneBaseline.dta", clear

************************************************
* Factors of Production
************************************************

	est clear
	
	// Create Total Costs
	foreach survey in bl el {
		local plant_varlist plant_cost_labor_`survey' plant_cost_elec_`survey' plant_cost_material_`survey'
		local bh_varlist bh_cost_capital_`survey' bh_cost_labor_`survey' bh_cost_fuel_`survey' bh_cost_material_`survey'
		
		gen plant_cost_total_`survey' = 0, before(plant_cost_capital_el)
		gen bh_cost_total_`survey' = 0, before(bh_cost_capital_el)
		foreach var of varlist `plant_varlist' {
			replace plant_cost_total_`survey' = plant_cost_total_`survey' + `var'
		}
		foreach var of varlist `bh_varlist' {
			replace bh_cost_total_`survey' = bh_cost_total_`survey' + `var'
		}
	}
	
	// Run regressions: non-BH
		
	* w/ control
	foreach var in total elec labor material {
		eststo reg_plant_`var': reg plant_cost_`var'_el treat plant_cost_`var'_bl, robust
		quietly sum plant_cost_`var'_el if treat==0 & !missing(plant_cost_`var'_el) & !missing(plant_cost_`var'_bl)
		estadd scalar mean = `r(mean)'
		estadd local ctrl_mean = string(r(mean), "%15.2f")
	}
	* blank cols
	foreach var in capital fuel {
		eststo reg_plant_`var': reg plant_cost_`var'_el, robust
		estadd local N = "", replace
		estadd local r2 = "", replace
	}
	
	
	// Run regressions - BH
	
	* w/ control 
	foreach var in total capital labor fuel material {
		eststo reg_bh_`var': reg bh_cost_`var'_el treat bh_cost_`var'_bl, robust
		sum bh_cost_`var'_el if treat==0 & !missing(bh_cost_`var'_el) & !missing(bh_cost_`var'_bl)
		estadd scalar mean = `r(mean)'
		estadd local ctrl_mean = string(r(mean), "%15.2f")
	}
	* blank cols
	foreach var in elec {
		eststo reg_bh_`var': reg bh_cost_`var'_el, robust
		estadd local N = "", replace
		estadd local r2 = "", replace
	}
				
********************************************************************
* Abatement Devices (Count + Any + Cost)
********************************************************************


	//	Create total number of APCDs and dummy for any APCDs 
	foreach survey in bl el {
		gen apcds_total_cost_`survey' = cyclones_total_cost_`survey' + bagfilters_total_cost_`survey' + scrubbers_total_cost_`survey' + esps_total_cost_`survey', after(esps_total_cost_`survey')
		gen num_apcds_`survey' = num_cyclones_`survey' + num_scrubbers_`survey' + num_bagfilters_`survey' + num_esps_`survey', after(num_esps_`survey')
		gen D_apcds_`survey' = (num_apcds_`survey' > 0), after(num_apcds_`survey')
		replace D_apcds_`survey' = . if num_apcds_`survey'==.
	}	

	
	// Run Regressions 
	foreach var in apcds cyclones bagfilters scrubbers esps {
		* count
		eststo reg_num_`var': reg num_`var'_el treat num_`var'_bl, robust
		sum num_`var'_el if treat==0
		estadd scalar mean = `r(mean)'
		estadd local ctrl_mean = string(r(mean), "%15.2f")
		
		* cost
		eststo reg_cost_`var': reg `var'_total_cost_el treat `var'_total_cost_bl, robust
		sum `var'_total_cost_el if treat==0
		estadd scalar mean = `r(mean)'
		estadd local ctrl_mean = string(r(mean), "%15.2f")
	}
	
	foreach var in apcds cyc bf scr esp {
		* any
		eststo reg_D_`var': reg D_`var'_el treat D_`var'_bl, robust
		sum D_`var'_el if treat==0
		estadd scalar mean = `r(mean)'
		estadd local ctrl_mean = string(r(mean), "%15.2f")
	}
	
	
	//	Create Tables
	
local titles "&\multicolumn{1}{c}{All} &\multicolumn{1}{c}{Cyclone} &\multicolumn{1}{c}{Bag} &\multicolumn{1}{c}{Scrubber} &\multicolumn{1}{c}{ESP} &\multicolumn{1}{c}{Total} &\multicolumn{1}{c}{Capital} &\multicolumn{1}{c}{Labor} &\multicolumn{1}{c}{Electricity} &\multicolumn{1}{c}{Fuel}  &\multicolumn{1}{c}{Materials} \\"
local numbers "&\multicolumn{1}{c}{(1)} &\multicolumn{1}{c}{(2)} &\multicolumn{1}{c}{(3)} &\multicolumn{1}{c}{(4)} &\multicolumn{1}{c}{(5)} &\multicolumn{1}{c}{(6)} &\multicolumn{1}{c}{(7)} &\multicolumn{1}{c}{(8)} &\multicolumn{1}{c}{(9)} &\multicolumn{1}{c}{(10)}  &\multicolumn{1}{c}{(11)} \\"                                                           


# delimit ;

esttab reg_cost_apcds reg_cost_cyclones reg_cost_bagfilters reg_cost_scrubbers reg_cost_esps reg_bh_total reg_bh_capital reg_bh_labor reg_plant_elec reg_bh_fuel reg_bh_material
	using "$PHONE_TABS/Table_5.tex", replace 
	se ar2 booktabs varwidth(32)
	keep(treat) coeflabel(treat "ETS Treatment (=1)")
	mgroups("\shortstack{Abatement capital costs (\\$1000s)}" "\shortstack{Boiler house input costs (\\$1000s)}", 
	pattern(1 0 0 0 0 1 0 0 0 0 0)
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
	prehead("{" "\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" "\begin{tabular}{l*{11}{c}}" "\toprule")
	posthead("`titles'" "`numbers'" "\midrule \addlinespace")
	prefoot("\addlinespace \addlinespace")
	star(* 0.10 ** 0.05 *** 0.01) nonotes nonumbers nomtitles
	stats(r2 ctrl_mean N, label("R\textsuperscript{2}" "Control mean" "Plants") fmt(%9.2f %9.2f %1s))
	;

#delimit cr



local super_title "&  & \multicolumn{4}{c}{Components} \\  \cmidrule{3-6}"
local titles "& \multirow{-2}{*}{\shortstack{All \\ APCDs}}  & Cyclone & Bag & Scrubber & ESP \\"
local numbers "& (1) & (2) & (3) & (4) & (5) \\"


# delimit ;

esttab reg_D_apcds reg_D_cyc reg_D_bf reg_D_scr reg_D_esp 
	using "$PHONE_TABS/Table_F2.tex", replace 
	se r2 booktabs varwidth(32) 		
	drop(_cons *_bl) coeflabel(treat "ETS Treatment (=1)")
	nomtitles nonumbers fragment
	star(* 0.10 ** 0.05 *** 0.01)
	stats(r2 ctrl_mean N, label("R\textsuperscript{2}" "Control mean" "Plants") fmt(%9.2f %9.2f %1s))
	prefoot(\addlinespace\addlinespace)
	nonotes
	prehead("{" "\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" "\begin{tabular}{l*{5}{c}}" "\toprule")
	posthead("`super_title'" "`titles'" "`numbers'" "\midrule \addlinespace")
    postfoot("\bottomrule" "\end{tabular}""}")
	;
	
#delimit cr
	
	
est clear
